#include "strix.hpp"

Strix::Strix ()
{
  this->pref.define ("i", "", false, PrefDef::PT_STR,  "", "network Interface");
  this->pref.define ("d", "", false, PrefDef::PT_BOOL, "", "enable debug message");
  this->pref.define ("r", "", false, PrefDef::PT_STR,  "", "pcap foramt file (for test)");
  this->pref.define ("t", "", false, PrefDef::PT_BOOL, "", "config & rule test");

  assert (this->ethAddr = this->dec.makeFilter ("ether.src_addr"));
}

bool Strix::exec (int argc, char * argv[])
{
  try 
    {
      if (! this->pref.import (argc, argv))
	throw pref.getError ();

      // setup: debug message enable/disable by '-d' opiton
      if (this->pref.lookup ("d") > 0)
	enableDebugMsg ();
      else
	disableDebugMsg ();

      // setup: select input data source for network decoder
      if (0 < this->pref.lookup ("r"))
	{
	  if (false == this->dec.setupFile (pref.getStr ("r", 0)))
	    throw string (this->dec.getErrMsg ());
	}
      else if (0 < this->pref.lookup ("i"))
	{
	  if (false == this->dec.setupDevice (pref.getStr ("i", 0)))
	    throw string (this->dec.getErrMsg ());
	}
      else
	throw string ("You have to specify pcap data ('-r') or interface ('-i')." );

      // setup: test run
      if (pref.lookup ("t") > 0)
	this->testRun = true;

      // start: packet capture
      NDecResult * res;
      while (NULL != (res = this->dec.fetch ()))
	{
	  if (this->ethAddr->exec (*res))
	    {
	      void * addr = static_cast <u_int8_t*> (this->ethAddr->getResult ());
	    }
	}
    }
  catch (const string e)
    {
      cerr << "strix setup error: " << e << endl;
      return false;
    }

  return true;
}
